package com.jzo2o.customer.controller.consumer;

import cn.hutool.jwt.JWT;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author DarkClown
 * @date 2024-08-17 19:32
 * @project_name jzo2o-customer
 * @parem
 **/
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址相关接口")
public class ConAddressBookController {
    @Autowired
    IAddressBookService addressBookService;

    @Autowired
    JwtTool jwtTool;
    @GetMapping("/page")
    @ApiOperation("获取地址分页")
    public PageResult<AddressBook> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO, @RequestHeader("Authorization")String token) {

        //解析token
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);

        //通过用户Id查询地址列表
        return addressBookService.selectByUserId(addressBookPageQueryReqDTO,currentUserInfo.getId());

    }


    @PostMapping
    @ApiOperation("新增地址")
    public void add(@RequestBody AddressBook addressBook, @RequestHeader("Authorization")String token) {

        addressBookService.add(addressBook,token);

    }

    @GetMapping("/{id}")
    @ApiOperation("地址簿详情接口")
    public AddressBook addressBookById(@PathVariable("id")Long id){
        return addressBookService.addressBookById(id);
    }

    /**
     * 修改接口
     * @param addressBook
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation("地址不修改接口")
    public AddressBook updateAddressBook(@RequestBody AddressBook addressBook,@PathVariable("id")Long id){
        addressBook.setId(id);
        return addressBookService.updateAddressBook(addressBook);
    }

    /**
     * 删除
     */
    @DeleteMapping("/batch")
    @ApiOperation("地址删除接口")
    public AddressBook delete(@RequestBody String[] ids){
        return addressBookService.deleteByIds(ids);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    @ApiOperation("设置/取消默认地址")
    public AddressBook setDefault(Long id,Integer flag){
        return addressBookService.setDefault(id,flag);
    }
    /**
     * 获取用户默认地址
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("获取用户默认地址")
    public AddressBook getDefaultAddress(@RequestHeader("Authorization")String token){
        return addressBookService.getDefaultAddress(token);
    }


}
